
#include "BookBorrowing.h"
#include "BookBorrowingList.h"
#include <iostream>
#include <fstream>
#include "BookList.h"
#include "UserList.h"
using namespace std;



BookBorrowingList::BookBorrowingList()
{
	head = NULL;
	loadBorrowBook();
}

void BookBorrowingList::addBorrowBooktoList(char *username,int status,char* name,char* ISBN)
{
	BookBorrowingInfo temp;

	temp.addInfo(username,status,name,ISBN);
	
	ofstream file;

	file.open("BorrowBookInfo.bin",ios::binary | ios::app | ios::ate);

	file.write((char*)&temp,sizeof(BookBorrowingInfo));

	file.close();
}


void BookBorrowingList::updateBorrowBooktoList()
{
	Node *cur = head;

	BookBorrowingInfo temp;

	ofstream file;

	file.open("BorrowBookInfo.bin",ios::binary);

	while (cur)
	{
		temp.copyInfo(cur->data);
		file.write((char*)&temp,sizeof(BookBorrowingInfo));
		cur = cur->next;
	}

	file.close();
}

void BookBorrowingList::loadBorrowBook()
{
	freeBookBorrowing();
	Node *cur = head;
	BookBorrowingInfo buffer;
	ifstream file;
	file.open("BorrowBookInfo.bin",ios::binary);
	while (file.read((char*)&buffer,sizeof(buffer)))
	{
		Node *newNode = new Node;
		newNode->data.copyInfo(buffer);
		newNode->next = NULL;
		if (head == NULL)
		{
			head = newNode;
		}
		else
		{
			cur = head;
			while(cur->next)
			{
				cur = cur->next;
			}
			cur->next = newNode;
		}
	}

	file.close();
}

void BookBorrowingList::borrowBook()
{
	char username[100];
	cout << "Reader's username: ";
	cin.getline(username,sizeof(username));

	Node *cur = head;
	while (cur)
	{
		if (strcmp(cur->data.getUsername(),username)==0)
		{
			if (cur->data.getStatus() == 0)
			{
				cout << "This account is inactivated\n";
				return;
			}
			else if (cur->data.getStatus() == 2)
			{
				cout << "This account has been banned\n";
			}
			else if (cur->data.isFullBB() == 5)
			{
				cout << "This account has already borrowed 5 books\n";
			}
			else if (cur->data.getStatus() == 1 && cur->data.isFullBB() != 5)
			{
				int i = cur->data.isFullBB();
				char ISBN[14];
				cout << "ISBN: ";
				cin.getline(ISBN,sizeof(ISBN));

				BookList b;
				if(i == 5)
				{
					cout << "You have already are borrowing 5 books";
				}
				else if(b.inStock(ISBN) > 0)
				{
					cout << "This book is available\n";
					b.decreaseStock(ISBN);
					cur->data.addInfo(i,b.getNamebyISBN(ISBN),ISBN);
					updateBorrowBooktoList();
					cout << "Borrowing book successful\n";
					//cout << "The deadline of this book: " ;
					//cur->data.showDeadline(i);
					return;
				}
				else
				{
					cout << "This book is not available\n";
					//Search nearest deadline here
				}
			}
		}
		cur = cur->next;
	}

	//When first time borrow a book
	UserList a;
	BookList b;
	a.loadUserList(); //Load list of user;
	if (a.getStatusbyUsername(username) == 1)
	{
		char ISBN[14];
		cout << "ISBN: ";
		cin.getline(ISBN,sizeof(ISBN));

		BookList b;
		if(b.inStock(ISBN) > 0)
		{
			cout << "This book is available\n";
			b.decreaseStock(ISBN);
			addBorrowBooktoList(username,1,b.getNamebyISBN(ISBN),ISBN);
			cout << "Borrowing book successful\n";
			cout << "The deadline of this book: " ;
			loadBorrowBook();
			Node *cur = head;
			while (cur)
			{
				if(strcmp(cur->data.getUsername(),username) == 0)
				{
					cur->data.showDeadline(0);
					return;
				}
				cur = cur->next;
			}
		}
		else
		{
			cout << "This book is not available\n";
			//Search nearest deadline here
		}
	}
	else if (a.getStatusbyUsername(username) == 0)
	{
		cout << "This account is inactivate\n";
	}
	else
	{
		cout << "This account has been banned\n";
	}
}

void BookBorrowingList::returnBookbyUsername()
{
	char username[100];
	cout << "Reader's username: ";
	cin.getline(username,sizeof(username));

	Node *cur = head;
	while (cur)
	{
		if (strcmp(cur->data.getUsername(),username) == 0)
		{
			cur->data.showBorrowingBook();

			char ISBN[14];
			cout << "ISBN: ";
			cin.getline(ISBN,sizeof(ISBN));

			int i = cur->data.returnBook(ISBN);
			if (i != 5)
			{
				cur->data.updateReturnedBook(i);
				cur->data.showDayreturn(i);
			}
			cout << "Return book successful\n";
			updateBorrowBooktoList();
			loadBorrowBook();
			return;
		}
		cur = cur->next;
	}

}

//Show borrowing books of a username
void BookBorrowingList::showBorrowingBook(char *username)
{
	if (head == NULL)
	{
		cout << "You haven't borrowed any book\n";
	}
	else
	{
		Node *cur = head;
		while (cur)
		{
			if (strcmp(cur->data.getUsername(),username) == 0)
			{
				cur->data.showBorrowingBook();
				return ;
			}
			cur = cur->next;
		}
	}
}

//Show returned books of a username
void BookBorrowingList::showReturnedBook(char *username)
{
	if (head == NULL)
	{
		cout << "You haven't returned any book\n";
	}
	Node *cur = head;
	while (cur)
	{
		if (strcmp(cur->data.getUsername(),username) == 0)
		{
			cur->data.showReturnedBook();
			return ;
		}
		cur = cur->next;
	}
}

void BookBorrowingList::freeBookBorrowing()
{
	Node *cur = head;
	while (head)
	{
		head = head->next;
		delete cur;
		cur = head;
	}
}


